<template>
  <div class="contianer">
    <div class="header">
      <el-input
        v-model="input"
        placeholder="请输入菜品名称"
        clearable
        @keyup.enter.native="init"
      >
        <i
          slot="prefix"
          class="el-input__icon el-icon-search"
          @click="init"
        ></i>
      </el-input>
      <div class="right-actions">
        <el-link type="primary" @click="handleDelete(multipleSelection)"
          >批量删除</el-link
        >
        <el-divider direction="vertical"></el-divider>
        <el-link type="primary" @click="handleBatchChangeStatus(multipleSelection, 1)"
          >批量启售</el-link
        >
        <el-divider direction="vertical"></el-divider>
        <el-link type="primary" @click="handleBatchChangeStatus(multipleSelection, 0)"
          >批量停售</el-link
        >
        <el-button icon="el-icon-plus">新建菜品</el-button>
      </div>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="name" label="菜品名称"> </el-table-column>
      <el-table-column prop="image" label="图片">
        <template v-slot="{ row }">
          <el-image
            style="width: 50px; height: 50px"
            :src="`${env}/common/download?name=${row.image}`"
            :preview-src-list="[`${env}/common/download?name=${row.image}`]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="categoryName" label="菜品分类"> </el-table-column>
      <el-table-column prop="price" label="售价"> </el-table-column>
      <el-table-column label="售卖状态">
        <template v-slot="{ row }">
          {{ row.status ? "启售" : "停售" }}
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="最后操作时间">
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link type="primary">修改</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="确认调整该菜品的状态?"
            @onConfirm="handleChangeStatus(row)"
          >
            <el-link type="danger" slot="reference">{{
              row.status ? "停售" : "启售"
            }}</el-link>
          </el-popconfirm>
          <el-divider direction="vertical"></el-divider>
          <el-link type="primary" @click="handleDelete([row])">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getDish, changeStatus, deleteDishes } from "@/api/dish";
export default {
  data() {
    return {
      env: process.env.VUE_APP_BASE_API,
      input: "",
      tableData: [],
      multipleSelection: [],
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      getDish({ ...this.pageInfo, name: this.input }).then((res) => {
        const { records, total } = res.data;
        this.tableData = records;
        this.total = total;
      });
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
    async handleChangeStatus(row) {
      await changeStatus({ id: row.id, status: +!row.status });
      this.$message.success("改变成功");
      this.init();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    async handleDelete(array) {
      if (array.length === 0) return this.$message.warning("请选择要删除的菜品");
      try {
        await deleteDishes({
          ids: array.map(item => item.id).join(',')
        });
        this.$message.success("删除成功");
        this.init();
      } catch (error) {
        this.$message.error("删除失败");
      }
    },
    async handleBatchChangeStatus(selection, status) {
      if (selection.length === 0) {
        return this.$message.warning("请选择要操作的菜品");
      }
      try {
        for (const item of selection) {
          await changeStatus({ id: item.id, status });
        }
        this.$message.success("批量操作成功");
        this.init();
      } catch (error) {
        this.$message.error("批量操作失败");
      }
    }
  },
};
</script>

<style lang="scss" scoped>
.contianer {
  padding: 33px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .el-input {
      width: 250px;
      i {
        cursor: pointer;
      }
    }

    .right-actions {
      display: flex;
      align-items: center;

      .el-link {
        padding: 0 16px;
      }

      .el-divider {
        margin: 0;
        height: 1em;
        background-color: #dcdfe6;
      }

      .el-button {
        margin-left: 16px;
      }
    }
  }

  .footer {
    margin-top: 22px;
    display: flex;
    justify-content: center;
  }
}
</style>







